package m5;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 连接消息服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");   // wht6.cn
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        // 定义TOPIC类型交换机： topic_logs
        c.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);

        // 向交换机发送消息，并携带路由键
        while (true) {
            System.out.print("输入消息： ");
            String s = new Scanner(System.in).nextLine();
            System.out.print("输入路由键： ");
            String key = new Scanner(System.in).nextLine();
            /*
            第二个参数是路由键关键词，通过这个关键词来确定，消息会被发送到哪个队列
             */
            c.basicPublish("topic_logs", key, null, s.getBytes());
        }

    }
}
